/*******************************************************************************
 * @brief: 命令行支持
 * @Author:   aple
 * @Date: 2022-09-02 23:35:00
 * @LastEditors: aple
 * @LastEditTime: 2022-10-22 11:40:31
 * @Verision: 1.0.0.0
 ******************************************************************************/
#include "framework.h"
#include "str_ext.h"

/*******************************************************************************
 * @brief: 功能示例
 * @param argc 参数数量
 * @param argv 参数指针
 * @return 错误代码
 ******************************************************************************/
int func(int argc, char *argv[])
{
	dbg_puts("func has been called! \n");
	return 0;
}
DBG_FUN_EXPORT(func);


/*******************************************************************************
 * @brief: shell config
 ******************************************************************************/
static char *argv_buff[SHELL_ARGV_MAX + 1];
static char cmd_buff[SHELL_CMD_BUFF_SIZE + 1];
const shell_def_type sh_obj = {
	argv_buff,
	cmd_buff,
	SHELL_ARGV_MAX,
	SHELL_CMD_BUFF_SIZE};

/*******************************************************************************
 * @brief: shell init
 ******************************************************************************/
void shell_init(void)
{
	shell_config(&sh_obj);
}

/*******************************************************************************
 * @brief: shell 功能对象
 * @param none
 * @return {*}
 ******************************************************************************/
void shell_iter_obj(volatile const func_info_type *volatile fun, char c)
{
	if (c == 'h')
	{
		if (fun->txt)
		{
			sh_print("SHELL CMD :: Address = %08X, Name = %s %s" PTCR, (int)fun->func, fun->name, fun->txt);
		}
		else
		{
			sh_print("SHELL CMD :: Address = %08X, Name = %s" PTCR, (int)fun->func, fun->name);
		}
	}
	else
	{
		if (fun->txt)
		{
			sh_print("SHELL CMD :: Name = %s %s" PTCR, fun->name, fun->txt);
		}
		else
		{
			sh_print("SHELL CMD :: Name = %s" PTCR, fun->name);
		}
	}
}

/*******************************************************************************
 * @brief: shell 分支
 * @param none
 * @return {*}
 ******************************************************************************/
void shell_case(char c)
{
#if DBG_EN == 0
	if (c == 'h')
		c = 'H';
#endif

	switch (c)
	{
	case 'h':
	{
		shell_iter(shell_iter_obj, 'h');

		break;
	}
	case 'H':
	{
		shell_iter(shell_iter_obj, 'H');
		break;
	}

	default:
		break;
	}
}

/*******************************************************************************
 * @brief: 显示命令列表
 * @param argc 参数数量
 * @param argv 参数指针
 * @return 错误代码
 ******************************************************************************/
SHELL_FUNX_EXPORT(shell, "-H/h");
int shell(int argc, char *argv[])
{
	sh_puts(SYMBOL_LINE_EQ PTCR);

	for (int var = 1; var < argc; var++)
	{
		char *str = argv[var];

		if (str[0] == '-')
		{
			shell_case(str[1]);
		}
		else if (str[0] == '?')
		{
			sh_puts("-h: cmd list with address for debug mode" PTCR "-H: cmd only" PTCR);
		}
	}

	sh_puts(SYMBOL_LINE_EQ PTCR);

	return 0;
}
